package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.model.OrganizaModel;
import com.jp.knowledge.util.ResultSubscriber;

import org.xutils.view.annotation.ViewInject;

/**
 * Created by 528787067@qq.com on 2017/3/27.
 */

public class UserSettingEditCompany extends SlidingActivity implements View.OnClickListener, ResultSubscriber.OnResultListener {

    private static final int DEPARTMENT_SETTING_CODE = 0x01;

    private Intent intent;
    private OrganizaModel organizaModel;
    private String departmentId;
    private String positionName;

    @ViewInject(R.id.user_setting_item_department)
    private LinearLayout departmentItem;
    @ViewInject(R.id.user_setting_item_department_desc)
    private TextView departmentDesc;
    @ViewInject(R.id.user_setting_item_position)
    private LinearLayout positionItem;
    @ViewInject(R.id.user_setting_item_position_desc)
    private TextView positionDesc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.intent = getIntent();
        this.organizaModel = (OrganizaModel) intent.getSerializableExtra("data");
        this.departmentId = organizaModel.getDepartmentId();
        this.positionName = organizaModel.getPosition();
        if(organizaModel == null){
            ToasUtil.toast(this, "公司信息有误");
            finish();
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getResourceId() {
        return R.layout.activity_user_setting_edit_company;
    }

    @Override
    protected void init() {
        topName.setText(organizaModel.getCompanyName());
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        topDesc.setVisibility(View.GONE);
        rightIcon.setVisibility(View.GONE);
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setText("保存");

        departmentDesc.setText(organizaModel.getDepartment());
        positionDesc.setText(organizaModel.getPosition());

        leftIcon.setOnClickListener(this);
        rightBtn.setOnClickListener(this);
        departmentItem.setOnClickListener(this);
        positionItem.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != RESULT_OK)
            return;
        if(requestCode == DEPARTMENT_SETTING_CODE){
            organizaModel.setDepartmentId(data.getStringExtra(UserSettingDepartmentActivity.SELECTED_DEPARTMENT_ID));
            organizaModel.setDepartment(data.getStringExtra(UserSettingDepartmentActivity.SELECTED_DEPARTMENT_NAME));
            departmentDesc.setText(organizaModel.getDepartment());
        } else if(requestCode == UserSettingEditActivity.REQUEST_CODE){
            organizaModel.setPosition(data.getStringExtra(UserSettingEditActivity.RESULT_DATA));
            positionDesc.setText(organizaModel.getPosition());
        }
    }

    private void updateOrganize(){
        JsonObject param = new JsonObject();
        param.addProperty("id", organizaModel.getCompanyId());
        param.addProperty("type", organizaModel.getCompanyType());
        param.addProperty("departmentId", organizaModel.getDepartmentId());
        param.addProperty("position", organizaModel.getPosition());
        HttpHelper.getInstance(this).updateOrganize(param, 0x01, this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.icon_left:
                finish();
                break;
            case R.id.right_bn:
                if(departmentId.equals(organizaModel.getDepartmentId())
                        && positionName.equals(organizaModel.getPosition()))
                    finish();
                else
                    updateOrganize();
                break;
            case R.id.user_setting_item_department:
                Intent intent = new Intent(mContext, UserSettingDepartmentActivity.class);
                intent.putExtra("company",organizaModel);
                intent.putExtra("userEdit",true);
                startActivityForResult(intent, DEPARTMENT_SETTING_CODE);
                break;
            case R.id.user_setting_item_position:
                UserSettingEditActivity.gotoEdit(this, R.id.user_setting_item_position,
                        "设置职位", organizaModel.getPosition(), "输入你所在的职位");
                break;
        }
    }

    @Override
    public void onStart(int requestType) {
        loading.show();
    }
    @Override
    public void onCompleted(int requestType) {
        loading.cancel();
    }
    @Override
    public void onError(int requestType) {
        ToasUtil.toast(this, "组织信息修改失败");
        onCompleted(requestType);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0) {
            onError(requestType);
        } else{
            setResult(RESULT_OK, intent.putExtra("data", organizaModel));
            finish();
        }
    }
}
